package com.ac.scum.log.web.model.player.service;

import com.ac.scum.log.bean.Player;
import com.ac.scum.log.web.model.player.mapper.PlayerBalanceMapper;
import com.ac.scum.player.bean.PlayerBalance;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
public class PlayerBalanceService extends ServiceImpl<PlayerBalanceMapper, PlayerBalance> {

    private final PlayerService playerService;

    public PlayerBalance findPlayerBalance(Long serverId, Long playerId) {
        PlayerBalance playerBalance = this.getBaseMapper().selectList(Wrappers.<PlayerBalance>lambdaQuery()
                .eq(PlayerBalance::getServerId, serverId)
                .eq(PlayerBalance::getPlayerId, playerId)).stream().findFirst().orElse(null);
        // 如果玩家余额为空，则初始化玩家余额
        if (playerBalance == null) {
            Player player = playerService.findPlayerById(serverId, playerId);
            playerBalance = new PlayerBalance();
            playerBalance.setPlayerId(player.getId());
            playerBalance.setPlayerSteamId(player.getSteamId());
            playerBalance.setServerId(serverId);
            this.save(playerBalance);
        }
        return playerBalance;
    }
}
